package com.example.qr_readerexample;

import com.dlazaro66.qrcodereaderview.QRCodeReaderView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.os.ProcessManager;

import java.util.logging.Logger;

public class MainAbility extends Ability implements QRCodeReaderView.OnQRCodeReadListener {
    private QRCodeReaderView mQrCodeReaderView;
    private Checkbox mCheckBox_1;
    private Checkbox mCheckBox_2;
    private PointsOverlayView pointsOverlayView;
    private Text resultTextView;
    private static final String TAG = "MainAbility";
    private static final int REQUEST_CAMERA_PERMISSION = 101;
    private boolean isQRChecked = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        WindowManager windowManager = WindowManager.getInstance();
        Window window = windowManager.getTopWindow().get();
        window.setTransparent(true);

        mQrCodeReaderView = (QRCodeReaderView) findComponentById(ResourceTable.Id_qrcoderView);
        mQrCodeReaderView.getSurfaceOps().get().addCallback(mQrCodeReaderView);
        mQrCodeReaderView.setOnQRCodeReadListener(this);

        mCheckBox_1 = (Checkbox) findComponentById(ResourceTable.Id_check_box_1);
        mCheckBox_1.setClickedListener(clickedListener1);

        mCheckBox_2 = (Checkbox) findComponentById(ResourceTable.Id_check_box_2);
        mCheckBox_2.setClickedListener(clickedListener2);

        resultTextView = (Text) findComponentById(ResourceTable.Id_txt);
        pointsOverlayView = (PointsOverlayView) findComponentById(ResourceTable.Id_point_view);


        String[] permissions = {"ohos.permission.CAMERA"};
        requestPermissionsFromUser(permissions, REQUEST_CAMERA_PERMISSION);

    }

    Component.ClickedListener clickedListener1 = new Component.ClickedListener() {
        @Override
        public void onClick(Component component) {
            mQrCodeReaderView.setTorchEnabled(!mCheckBox_1.isChecked());
        }
    };


    Component.ClickedListener clickedListener2 = new Component.ClickedListener() {
        @Override
        public void onClick(Component component) {
            isQRChecked = true;

            if (!mCheckBox_2.isChecked()) {
                mQrCodeReaderView.doDecode();
            } else {
                pointsOverlayView.setVisibility(Component.INVISIBLE);
                mQrCodeReaderView.stopDecode();
            }
        }
    };


    @Override
    protected void onActive() {
        super.onActive();
        Logger.getLogger(TAG).severe("onActive  mQrCodeReaderView =" + mQrCodeReaderView);

    }


    @Override
    protected void onInactive() {
        super.onInactive();
        Logger.getLogger(TAG).severe("onInactive  mCheckBox_2.isChecked() =" + mCheckBox_2.isChecked());
        if(isQRChecked){
            Logger.getLogger(TAG).severe("kill " );
            ProcessManager.kill(ProcessManager.getPid());
        }

    }

    @Override
    protected void onBackground() {
        super.onBackground();
        Logger.getLogger(TAG).severe("onBackground  mQrCodeReaderView =" + mQrCodeReaderView);
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_CAMERA_PERMISSION: {
                if(!mQrCodeReaderView.isStartingCamera()){
                    mQrCodeReaderView.startCamera();
                }
                Logger.getLogger(TAG).severe("MainAbility onRequestPermissionsFromUserResult ");
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mQrCodeReaderView != null) {
            mQrCodeReaderView.stopCamera();
        }
    }

    // Called when a QR is decoded
    // "text" : the text encoded in QR
    // "points" : points where QR control points are placed
    @Override
    public void onQRCodeRead(String text, Point[] points) {
        resultTextView.setText(text);
        pointsOverlayView.setVisibility(Component.VISIBLE);
        pointsOverlayView.setPoints(points);
        mQrCodeReaderView.stopDecode();
    }
}
